commonlibsse_ng\re\b/BSTList.rs
1use core::cmp::Ordering;
2use core::fmt;
3use core::hash::{Hash, Hasher};
4use core::marker::PhantomData;
5use core::ptr::NonNull;
6
7use crate::re::TESBox::TESBox;
8
9use std_fork::option::UntaggedOption;
10use std_fork::zeroable::Zeroable;
11
12/// A node in the singly linked list.
13///
14/// This structure holds an optional pointer to the value (`item`)
15/// and the next node (`next`) in the list. The pointers are managed through
16/// `TESBox`, which allocates memory from the custom memory manager.
17///
18/// # Safety
19/// The `item` and `next` are raw pointers that must be safely dropped using `TESBox`
20/// to avoid memory leaks. If not dropped correctly, memory leakage can occur.
21///
22/// # Note
23/// This list assumes that nodes with zero-initialized values (`item`) are considered "empty" nodes.
24/// You should not use such nodes directly as they might be unsafe unless they are properly initialized.
25/// This pattern is a consequence of handling raw pointers and being FFI-compatible.
26#[derive(PartialEq)]
27#[repr(C)]
28pub struct Node<T>
29where
30 T: Zeroable,
31{
32 /// The last pushed item of the list.
33 item: UntaggedOption<T>,
34
35 /// Pointer to the next node in the list.
36 /// # Note
37 /// The C++ implementation also defined it as next, so we follow it,
38 /// but it is actually the Node of the immediately **preceding pushed item**.
39 next: Option<NonNull<Node<T>>>,
40
41 /// The linked list uses raw pointers internally due to ownership issues.
42 ///
43 /// To begin with, `item` and `next` are heap-allocated data from `MemoryManager`, which is managed as `TESBox`. This marker represents the origin of those raw pointers.
44 ///
45 /// # Safety
46 /// If you don't drop them as `TESBox` at drop time, memory leakage will occur.
47 marker: PhantomData<TESBox<T>>,
48}
49const _: () = assert!(core::mem::size_of::<Node<i32>>() == 0x10);
50// const _: () = assert!(core::mem::size_of::<Node<*mut ()>>() == 0x18); // Size with enum tag
51const _: () = assert!(core::mem::size_of::<Node<*mut ()>>() == 0x10); // Size without enum tag
52
53impl<T> Node<T>
54where
55 T: Zeroable,
56{
57 /// Creates a new node containing the given item and pointing to the given next node.
58 ///
59 /// # Example
60 /// ```
61 /// # use commonlibsse_ng::re::BSTList::Node;
62 /// let node = Node::new(10, None);
63 /// ```
64 #[inline]
65 pub const fn new(item: T, next: Option<NonNull<Self>>) -> Self {
66 Self { item: UntaggedOption::some(item), next, marker: PhantomData }
67 }
68
69 /// Creates an empty node with no value and no next pointer.
70 ///
71 /// # Example
72 /// ```
73 /// # use commonlibsse_ng::re::BSTList::Node;
74 /// let empty_node = Node::<i32>::new_empty();
75 /// ```
76 #[inline]
77 pub const fn new_empty() -> Self {
78 Self { item: UntaggedOption::none(), next: None, marker: PhantomData }
79 }
80
81 /// Leaks the node onto the heap and returns a `NonNull` pointer to it.(To create a next node)
82 ///
83 /// This method changes the node's state to be managed by the `TESBox` memory manager.
84 ///
85 /// # Safety
86 /// The returned pointer must be eventually dropped using `TESBox` to avoid a memory leak.
87 fn leak(self) -> NonNull<Self> {
88 NonNull::from(TESBox::leak(TESBox::new(self)))
89 }
90}
91
92impl<T> Default for Node<T>
93where
94 T: Zeroable,
95{
96 #[inline]
97 fn default() -> Self {
98 Self::new_empty()
99 }
100}
101
102/// Single-directional linked list (stack-like).
103///
104/// This linked list behaves like a stack, where the most recently pushed value is always placed at the head.
105/// The previous head is stored on the heap as the "next" value, forming a chain of nodes.
106///
107/// Diagram of the linked list structure (Head -> Next -> Next...):
108///
109/// ```txt
110/// head -> [value] -> [next] -> [next] -> ... -> None
111/// ↑
112/// latest push
113/// ```
114///
115/// # Note
116/// This linked list assumes that a value is considered uninitialized when it is zero-initialized.
117/// Using it without zero initialization is dangerous.
118///
119/// Ideally, an `Option` should be used to indicate whether a value is initialized, but this risk persists due to the strict memory layout requirements imposed by the FFI (Foreign Function Interface) type.
120///
121/// # Example
122/// ```
123/// # use commonlibsse_ng::re::BSTList::BSSimpleList;
124/// let mut list = BSSimpleList::new();
125/// list.push_front(10);
126/// list.push_front(20);
127/// list.push_front(30);
128/// list.push_front(40);
129///
130/// list.print_tree();
131/// assert_eq!(list.len(), 4);
132///
133/// let mut iter = list.iter();
134/// assert_eq!(iter.next(), Some(&40));
135/// assert_eq!(iter.next(), Some(&30));
136/// assert_eq!(iter.next(), Some(&20));
137/// assert_eq!(iter.next(), Some(&10));
138///
139/// list.pop_front();
140///
141/// assert_eq!(list.len(), 3);
142/// let mut iter = list.iter();
143/// assert_eq!(iter.next(), Some(&30));
144/// assert_eq!(iter.next(), Some(&20));
145/// assert_eq!(iter.next(), Some(&10));
146/// ```
147#[repr(C)]
148pub struct BSSimpleList<T>
149where
150 T: Zeroable,
151{
152 /// The last pushed node of the list.
153 list_head: Node<T>,
154}
155
156impl<T> BSSimpleList<T>
157where
158 T: Zeroable,
159{
160 /// Creates a new, empty list.
161 ///
162 /// # Example
163 /// ```
164 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
165 /// let list = BSSimpleList::<i32>::new();
166 /// assert!(list.is_empty());
167 /// ```
168 #[inline]
169 pub const fn new() -> Self {
170 Self { list_head: Node::new_empty() }
171 }
172
173 /// Pushes a new value to the front of the list.
174 ///
175 /// # Example
176 /// ```
177 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
178 /// let mut list = BSSimpleList::new();
179 /// list.push_front(10);
180 /// ```
181 #[inline]
182 pub fn push_front(&mut self, value: T) {
183 if let Some(current_head) = self.list_head.item.take() {
184 let prev_node = Node::new(current_head, self.list_head.next).leak();
185 let new_node = Node::new(value, Some(prev_node));
186 self.list_head = new_node;
187 } else {
188 self.list_head.item = UntaggedOption::some(value);
189 }
190 }
191
192 /// Removes and returns the first element in the list.
193 ///
194 /// # Returns
195 /// `Some(TESBox<Node<T>>)` if the list is not empty.
196 /// `None` if the list is empty.
197 ///
198 /// # Example
199 /// ```
200 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
201 /// let mut list = BSSimpleList::new();
202 /// list.push_front(1);
203 /// assert!(list.pop_front().is_some());
204 /// ```
205 pub fn pop_front(&mut self) -> Option<T> {
206 let head = &mut self.list_head;
207 let last_item = head.item.take();
208
209 if let Some(next_node) = head.next {
210 let mut next_node = unsafe { TESBox::from_non_null(next_node) };
211 head.item = next_node.as_mut().item.take().into();
212 head.next = next_node.next;
213 drop(next_node);
214 };
215
216 last_item
217 }
218
219 /// Returns a reference to the front element.
220 ///
221 /// # Returns
222 /// `None` if the list is empty.
223 ///
224 /// # Example
225 /// ```
226 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
227 /// let list = BSSimpleList::<i32>::new();
228 /// assert!(list.front().is_none());
229 /// ```
230 #[inline]
231 pub fn front(&self) -> Option<&T> {
232 self.list_head.next.as_ref().and_then(|node| unsafe { node.as_ref().item.as_ref() })
233 }
234
235 /// Returns `true` if the list is empty.
236 ///
237 /// # Example
238 /// ```
239 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
240 /// let mut list = BSSimpleList::<i32>::new();
241 /// assert!(list.is_empty());
242 /// ```
243 #[inline]
244 pub const fn is_empty(&self) -> bool {
245 self.list_head.next.is_none()
246 }
247
248 /// Returns the number of elements in the list.
249 ///
250 /// Note that the computation cost is `O(n)` since it is a linked list.
251 ///
252 /// # Example
253 /// ```
254 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
255 /// let mut list = BSSimpleList::new();
256 /// list.push_front(1); // 2
257 /// list.push_front(2); // 1
258 /// list.push_front(3); // 0
259 ///
260 /// assert_eq!(list.len(), 3);
261 /// ```
262 #[inline]
263 pub fn len(&self) -> usize {
264 if self.list_head.item.is_none() {
265 return 0;
266 }
267
268 let mut len = 1; // count the first node.
269 let mut current = self.list_head.next.as_ref();
270 while let Some(node) = current {
271 len += 1;
272 current = unsafe { node.as_ref().next.as_ref() };
273 }
274 len
275 }
276
277 /// Inserts a new value after the given node.
278 ///
279 /// # Returns
280 /// A mutable reference to the inserted node, or `None` if insertion failed.
281 ///
282 /// # Example
283 /// ```
284 /// # use commonlibsse_ng::re::BSTList::{BSSimpleList, Node};
285 /// let mut list = BSSimpleList::new();
286 /// let mut first = Node::new(1, None);
287 /// list.insert_after(&mut first, 2);
288 /// ```
289 pub fn insert_after(&mut self, pos: &mut Node<T>, value: T) -> Option<&mut Node<T>> {
290 pos.next = Some(Node::new(value, pos.next.take()).leak());
291 pos.next.as_mut().map(|n| unsafe { n.as_mut() })
292 }
293
294 /// Removes the node after the given position.
295 ///
296 /// # Example
297 /// ```
298 /// use commonlibsse_ng::re::BSTList::{BSSimpleList, Node};
299 ///
300 /// let mut list = BSSimpleList::new();
301 /// let mut first = Node::new(1, None);
302 /// list.insert_after(&mut first, 2);
303 /// list.erase_after(&mut first);
304 /// ```
305 #[inline]
306 pub const fn erase_after(&mut self, pos: &mut Node<T>) {
307 if let Some(mut node) = pos.next.take() {
308 pos.next = unsafe { node.as_mut().next.take() };
309 }
310 }
311
312 /// Returns a reference to the node at the given position.
313 ///
314 /// # Example
315 /// ```
316 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
317 /// let mut list = BSSimpleList::new();
318 /// list.push_front(1); // 3
319 /// list.push_front(2); // 2
320 /// list.push_front(3); // 1
321 /// list.push_front(4); // 0
322 ///
323 /// assert_eq!(list.get(1), Some(&3));
324 /// ```
325 #[inline]
326 pub fn get(&self, pos: usize) -> Option<&T> {
327 for (idx, value) in self.iter().enumerate() {
328 if idx == pos {
329 return Some(value);
330 }
331 }
332
333 None
334 }
335
336 /// Returns a mutable reference to the node at the given position.
337 ///
338 /// # Example
339 /// ```
340 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
341 /// let mut list = BSSimpleList::new();
342 /// list.push_front(1); // 3
343 /// list.push_front(2); // 2
344 /// list.push_front(3); // 1
345 /// list.push_front(4); // 0
346 ///
347 /// if let Some(value) = list.get_mut(2) {
348 /// *value = 5;
349 /// }
350 /// assert_eq!(list.get(2), Some(&5));
351 /// ```
352 #[inline]
353 pub fn get_mut(&mut self, pos: usize) -> Option<&mut T> {
354 for (idx, value) in self.iter_mut().enumerate() {
355 if idx == pos {
356 return Some(value);
357 }
358 }
359
360 None
361 }
362
363 /// Clears the entire list.
364 #[inline]
365 pub fn clear(&mut self) {
366 let mut current = self.list_head.next.take();
367
368 self.list_head.item = UntaggedOption::none();
369
370 while let Some(node_ptr) = current {
371 unsafe {
372 let node = node_ptr.as_ref();
373 current = node.next;
374 drop(TESBox::from_raw(node_ptr.as_ptr()));
375 }
376 }
377 }
378
379 /// Returns an iterator over the list.
380 #[inline]
381 pub fn iter(&self) -> Iter<T> {
382 Iter {
383 fist_node_item: self.list_head.item.as_ref(),
384 current: self.list_head.next,
385 _marker: PhantomData,
386 }
387 }
388
389 /// Returns a mutable iterator over the list.
390 #[inline]
391 pub fn iter_mut(&mut self) -> IterMut<T> {
392 IterMut {
393 fist_node_item: self.list_head.item.as_mut(),
394 current: self.list_head.next,
395 _marker: PhantomData,
396 }
397 }
398
399 /// Consumes the `SimpleList` and returns a `Vec<T>` containing all elements in order.
400 ///
401 /// This method traverses the linked list and collects the values into a `Vec<T>`.
402 #[inline]
403 pub fn into_vec(self) -> Vec<T> {
404 let mut vec = Vec::new();
405 for node in self {
406 vec.push(node);
407 }
408 vec
409 }
410
411 /// Resizes the list to the given length.
412 ///
413 /// - If the list is shorter, it will be extended with the given value.
414 /// - If the list is longer, it will be truncated.
415 ///
416 /// # Example
417 /// - If length is 0, nothing is done.
418 /// ```
419 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
420 /// let mut list = BSSimpleList::<i32>::new();
421 /// list.resize(3, 0);
422 /// assert_eq!(list.len(), 0);
423 /// ```
424 ///
425 /// - If the list is longer, it will be truncated.
426 /// ```
427 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
428 /// let mut list = BSSimpleList::<i32>::new();
429 /// list.push_front(1);
430 /// list.push_front(2);
431 /// list.push_front(3);
432 /// list.push_front(4);
433 /// list.resize(3, 0);
434 /// assert_eq!(list.len(), 3);
435 /// ```
436 ///
437 /// - If the list is shorter, it will be extended with the given value.
438 /// ```
439 /// # use commonlibsse_ng::re::BSTList::BSSimpleList;
440 /// let mut list = BSSimpleList::<i32>::new();
441 /// list.push_front(1);
442 /// list.push_front(2);
443 /// list.resize(4, 5);
444 ///
445 /// assert_eq!(list.len(), 4);
446 ///
447 /// let mut iter = list.iter();
448 /// assert_eq!(iter.next(), Some(&5));
449 /// assert_eq!(iter.next(), Some(&5));
450 /// assert_eq!(iter.next(), Some(&2));
451 /// assert_eq!(iter.next(), Some(&1));
452 /// ```
453 #[inline]
454 pub fn resize(&mut self, new_size: usize, value: T)
455 where
456 T: Clone,
457 {
458 let this = &mut *self;
459 let current_len = this.len();
460
461 match new_size {
462 count if count < current_len => this.truncate(count),
463 count if count > current_len => this.grow(count - current_len, value),
464 _ => {}
465 }
466 }
467
468 #[inline]
469 fn grow(&mut self, count: usize, value: T)
470 where
471 T: Clone,
472 {
473 for _ in 0..count {
474 self.push_front(value.clone());
475 }
476 }
477
478 #[inline]
479 fn truncate(&mut self, count: usize) {
480 while self.len() > count {
481 self.pop_front();
482 }
483 }
484}
485
486////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
487
488pub struct Iter<'a, T>
489where
490 T: Zeroable,
491{
492 fist_node_item: Option<&'a T>,
493 current: Option<NonNull<Node<T>>>,
494 _marker: PhantomData<&'a T>,
495}
496
497impl<'a, T> Iterator for Iter<'a, T>
498where
499 T: Zeroable,
500{
501 type Item = &'a T;
502
503 fn next(&mut self) -> Option<Self::Item> {
504 if let Some(item) = self.fist_node_item.take() {
505 return Some(item);
506 }
507
508 self.current.take().and_then(|node_ptr| {
509 let node_ref = unsafe { node_ptr.as_ref() };
510 self.current = node_ref.next;
511 node_ref.item.as_ref()
512 })
513 }
514}
515
516pub struct IterMut<'a, T>
517where
518 T: Zeroable,
519{
520 fist_node_item: Option<&'a mut T>,
521 /// next node item
522 current: Option<NonNull<Node<T>>>,
523 _marker: PhantomData<&'a T>,
524}
525
526impl<'a, T> Iterator for IterMut<'a, T>
527where
528 T: Zeroable,
529{
530 type Item = &'a mut T;
531
532 fn next(&mut self) -> Option<Self::Item> {
533 if let Some(item) = self.fist_node_item.take() {
534 return Some(item);
535 }
536
537 self.current.take().and_then(|mut node_ptr| {
538 let node_ref = unsafe { node_ptr.as_mut() };
539 self.current = node_ref.next;
540 node_ref.item.as_mut()
541 })
542 }
543}
544
545/// An owning iterator over the elements of a `BSSimpleList<T>`.
546///
547/// This iterator consumes the list and yields each element by value.
548pub struct IntoIter<T>
549where
550 T: Zeroable,
551{
552 fist_node_item: Option<T>,
553 current: Option<NonNull<Node<T>>>,
554}
555
556impl<T> Iterator for IntoIter<T>
557where
558 T: Zeroable,
559{
560 type Item = T;
561
562 fn next(&mut self) -> Option<Self::Item> {
563 if let Some(item) = self.fist_node_item.take() {
564 return Some(item);
565 }
566
567 self.current.take().and_then(|node_ptr| {
568 let mut boxed = unsafe { TESBox::from_non_null(node_ptr) };
569 self.current = boxed.next;
570 boxed.item.take()
571 })
572 }
573}
574
575impl<T> IntoIterator for BSSimpleList<T>
576where
577 T: Zeroable,
578{
579 type Item = T;
580
581 type IntoIter = IntoIter<T>;
582
583 #[inline]
584 fn into_iter(self) -> Self::IntoIter {
585 let mut this = self; // To avoid mutable error.
586 IntoIter { fist_node_item: this.list_head.item.take(), current: this.list_head.next.take() }
587 }
588}
589
590impl<'a, T> IntoIterator for &'a BSSimpleList<T>
591where
592 T: Zeroable,
593{
594 type Item = &'a T;
595
596 type IntoIter = Iter<'a, T>;
597
598 #[inline]
599 fn into_iter(self) -> Self::IntoIter {
600 self.iter()
601 }
602}
603
604impl<'a, T> IntoIterator for &'a mut BSSimpleList<T>
605where
606 T: Zeroable,
607{
608 type Item = &'a mut T;
609
610 type IntoIter = IterMut<'a, T>;
611
612 #[inline]
613 fn into_iter(self) -> Self::IntoIter {
614 self.iter_mut()
615 }
616}
617
618impl<T> Extend<T> for &mut BSSimpleList<T>
619where
620 T: Zeroable,
621{
622 #[inline]
623 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
624 for item in iter {
625 self.push_front(item);
626 }
627 }
628}
629
630impl<T> Extend<T> for BSSimpleList<T>
631where
632 T: Zeroable,
633{
634 #[inline]
635 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
636 for item in iter {
637 self.push_front(item);
638 }
639 }
640}
641
642////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
643
644impl<T> Drop for BSSimpleList<T>
645where
646 T: Zeroable,
647{
648 #[inline]
649 fn drop(&mut self) {
650 self.clear();
651 }
652}
653
654impl<T> fmt::Debug for BSSimpleList<T>
655where
656 T: fmt::Debug + Zeroable,
657{
658 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
659 f.debug_list().entries(self.iter()).finish()
660 }
661}
662
663impl<T> Default for BSSimpleList<T>
664where
665 T: Zeroable,
666{
667 #[inline]
668 fn default() -> Self {
669 Self::new()
670 }
671}
672
673impl<T> Clone for BSSimpleList<T>
674where
675 T: Clone + Zeroable,
676{
677 #[inline]
678 fn clone(&self) -> Self {
679 let mut ret = Self::new();
680
681 let mut current = self.list_head.next.as_ref();
682 while let Some(node) = current {
683 let node = unsafe { node.as_ref() };
684 if let Some(item) = node.item.as_ref() {
685 ret.push_front(item.clone());
686 }
687 current = node.next.as_ref();
688 }
689
690 ret
691 }
692}
693
694impl<T> PartialEq for BSSimpleList<T>
695where
696 T: PartialEq + Zeroable,
697{
698 #[inline]
699 fn eq(&self, other: &Self) -> bool {
700 let mut a = self.iter();
701 let mut b = other.iter();
702 loop {
703 match (a.next(), b.next()) {
704 (Some(x), Some(y)) if x == y => {}
705 (None, None) => return true,
706 _ => return false,
707 }
708 }
709 }
710}
711impl<T> Eq for BSSimpleList<T> where T: Eq + Zeroable {}
712
713impl<T> PartialOrd for BSSimpleList<T>
714where
715 T: PartialOrd + Zeroable,
716{
717 #[inline]
718 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
719 let mut a = self.iter();
720 let mut b = other.iter();
721 loop {
722 match (a.next(), b.next()) {
723 (Some(x), Some(y)) => match x.partial_cmp(y) {
724 Some(Ordering::Equal) => {}
725 non_eq => return non_eq,
726 },
727 (None, None) => return Some(Ordering::Equal),
728 (None, _) => return Some(Ordering::Less),
729 (_, None) => return Some(Ordering::Greater),
730 }
731 }
732 }
733}
734
735impl<T> Ord for BSSimpleList<T>
736where
737 T: Ord + Zeroable,
738{
739 #[inline]
740 fn cmp(&self, other: &Self) -> Ordering {
741 let mut a = self.iter();
742 let mut b = other.iter();
743 loop {
744 match (a.next(), b.next()) {
745 (Some(x), Some(y)) => match x.cmp(y) {
746 Ordering::Equal => {}
747 non_eq => return non_eq,
748 },
749 (None, None) => return Ordering::Equal,
750 (None, _) => return Ordering::Less,
751 (_, None) => return Ordering::Greater,
752 }
753 }
754 }
755}
756
757impl<T> Hash for BSSimpleList<T>
758where
759 T: Hash + Zeroable,
760{
761 #[inline]
762 fn hash<H: Hasher>(&self, state: &mut H) {
763 for item in self.iter() {
764 item.hash(state);
765 }
766 }
767}
768
769impl<T> BSSimpleList<T>
770where
771 T: fmt::Display + Zeroable,
772{
773 /// Prints the list as a tree-like structure
774 pub fn print_tree(&self) {
775 /// Recursively prints each node in a tree-like structure
776 fn print_node<T>(node: &Node<T>, depth: usize)
777 where
778 T: fmt::Display + Zeroable,
779 {
780 if let Some(item) = node.item.as_ref() {
781 let indentation = " ".repeat(depth * 2);
782 println!("{indentation}- {item}");
783 if let Some(next_node) = &node.next {
784 // If there's a next node, print it recursively
785 unsafe {
786 let next_node_ref = next_node.as_ref();
787 print_node(next_node_ref, depth + 1);
788 }
789 }
790 }
791 }
792
793 print_node(&self.list_head, 0);
794 }
795}
796
797#[cfg(feature = "test_on_ci")]
798#[cfg(test)]
799mod tests {
800 use super::*;
801
802 #[test]
803 fn test_bs_simple_list() {
804 let mut list = BSSimpleList::new();
805 list.push_front(10);
806 list.push_front(20);
807 list.push_front(30);
808 list.push_front(40);
809
810 list.print_tree();
811 assert_eq!(list.len(), 4);
812
813 let mut iter = list.iter();
814 assert_eq!(iter.next(), Some(&40));
815 assert_eq!(iter.next(), Some(&30));
816 assert_eq!(iter.next(), Some(&20));
817 assert_eq!(iter.next(), Some(&10));
818
819 list.pop_front();
820
821 assert_eq!(list.len(), 3);
822 let mut iter = list.iter();
823 assert_eq!(iter.next(), Some(&30));
824 assert_eq!(iter.next(), Some(&20));
825 assert_eq!(iter.next(), Some(&10));
826 }
827
828 #[test]
829 fn test_resize() {
830 let mut list = BSSimpleList::<i32>::new();
831 list.resize(3, 0);
832 assert_eq!(list.len(), 0);
833
834 let mut list = BSSimpleList::<i32>::new();
835 list.push_front(1);
836 list.push_front(2);
837 list.push_front(3);
838 list.push_front(4);
839 list.resize(3, 0);
840 assert_eq!(list.len(), 3);
841 }
842}